package com.fram.service.api.v1.android.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.api.user.response.UserWalletResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.encryption.AESUtils;
import com.fram.entity.UserVirtualWallet;
import com.fram.entity.UserWithdrawCardNumber;
import com.fram.mapper.UserTransactionFlowMapper;
import com.fram.mapper.UserVirtualWalletMapper;
import com.fram.mapper.UserWithdrawCardNumberMapper;
import com.fram.service.api.v1.android.WalletAndroidService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class WalletAndroidServiceImpl implements WalletAndroidService {

	@Autowired
	private UserWithdrawCardNumberMapper userWithdrawCardNumberMapper;
	@Autowired
	private UserVirtualWalletMapper userVirtualWalletMapper;
	@Autowired
	private UserTransactionFlowMapper userTransactionFlowMapper;
	
	//展示钱包
	@Override
	public UserVirtualWallet exhibition(Long userId) throws ProException {
		UserVirtualWallet wallet = userVirtualWalletMapper.detailForAndroid(userId);
		if (wallet == null) {
			wallet = new UserVirtualWallet();
			wallet.setUserId(userId);
			wallet.setSign(PublicMethod.getMd5(wallet.getUserId() + "0.000.00", 1, true));
			if (userVirtualWalletMapper.insertSelective(wallet) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "创建钱包错误");
			}
		} else {
			// 拿到流水 总增加金额 不用判断
			System.out.println(""+userId);
			UserWalletResponse wall = userTransactionFlowMapper.totalRevenue(userId);
			System.out.println(""+wall);
			wallet.setBanlanceMoney(wall.getTotalRevenue().subtract(wall.getExpenditure()));
			// 重新加密
			wallet.setSign(PublicMethod.getMd5(
					wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true));
			if (userVirtualWalletMapper.updateByPrimaryKeySelective(wallet) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "流水今天修改到钱包里出错");
			}
			if (!wallet.getSign().equals(PublicMethod.getMd5(
					wallet.getUserId() + "" + wallet.getBanlanceMoney() + "" + wallet.getFrozenMoney(), 1, true))) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), "验证不通过，请联系管理员");
			}
		}
		return wallet;
	}

	@Override
	public void add(UserWithdrawCardNumber request) throws ProException {
		if (userWithdrawCardNumberMapper.insertSelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public void delete(Long id) throws ProException {
		if (userWithdrawCardNumberMapper.deleteByPrimaryKey(id) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	public PageInfo<UserWithdrawCardNumber> list(PageBean<UserWithdrawCardNumber> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<UserWithdrawCardNumber> list = userWithdrawCardNumberMapper.listForAndroid(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	public UserWithdrawCardNumber detail(Long id) throws ProException {
		UserWithdrawCardNumber request = userWithdrawCardNumberMapper.selectByPrimaryKey(id);
		if (request == null) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return request;
	}
	
	private static String key = "PAYPWD_AESUTISSS";
	@Override
	public void addPwd(UserVirtualWallet request) throws ProException {
		if (request.getPaypwd() != null) {
			try {
				request.setIsPaypwd(1);
				String AESPaypwd = AESUtils.encrypt(request.getPaypwd(), key);
				request.setPaypwd(PublicMethod.getMd5(AESPaypwd, 1, true));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (userVirtualWalletMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
